<?php
/**
 * created by zzy
 * date: 2017/11/21 10:39
 */

namespace App\Http\Admin\System\Controllers;

use Framework\BaseClass\Http\Admin\Controller;
use DB;
use App\Eloquent\Ygt\Company;
use App\Eloquent\Ygt\OrderType;
use App\Http\Admin\System\Models\Copy;


class CopyController extends Controller
{
    public function index()
    {
        $itemId                 = request('item_id', 1);
        $companyId              = request('company_id', 0);
        $company                = [];
        $where                  = [];
        $companyList            = Company::getList($where)->toArray();
        foreach( $companyList as $key=>$val ) {
            if ($val['id'] == $companyId) {
                $company = $val;
                unset($companyList[$key]);
            }
        }
        switch ($itemId)
        {
            case 1:
            case 3:
                $viewName       = 'department';
                break;
            case 2:
                $viewName       = 'process';
                break;
            default:
                $viewName       = 'department';
        }
        return $this->view($viewName, compact('company','companyList','itemId') );
    }
    public function processList()
    {
        $companyId              = request('from_company_id', 0);
        $where                  = ['company_id'=>$companyId];
        $collection             = OrderType::getList($where);
        $result                 = $collection->toJson();
        return $result;
    }
    public function copy()
    {
        $companyId              = request('company_id', 0);
        $fromCompanyId          = request('from_company_id', 0);
        $itemId                 = request('item_id', 0);
        if(!$companyId || !$fromCompanyId || !$itemId)
        {
            return $this->ajaxFail('参数不全');
        }
        if( $companyId==1 )
        {
            return $this->ajaxFail('当前厂不能是默认厂');
        }
        if( $companyId==$fromCompanyId )
        {
            return $this->ajaxFail('不支持同厂复制');
        }
        switch($itemId)
        {
            case 1:
                //企业模块
                //复制角色
                $tempResult     = Copy::privilege($companyId, $fromCompanyId);
                if( $tempResult == false )
                {
                    return $this->ajaxFail( '复制角色失败' );
                }
                //复制部门
                $tempResult     = Copy::department($companyId, $fromCompanyId);
                if( $tempResult == false )
                {
                    return $this->ajaxFail( '复制部门失败' );
                }
                break;
            case 2:
                //工序模块
                $orderTypeId    = request('id_arr', 0);
                if(!$companyId || !$fromCompanyId || !$itemId)
                {
                    return $this->ajaxFail('请选择要复制的工艺');
                }
                $tempResult     = Copy::orderTypeMore( $companyId, $fromCompanyId, $orderTypeId );
                if( $tempResult == false )
                {
                    return $this->ajaxFail( '复制失败' );
                }
                break;
            case 3:
                //材料模块
                $tempResult     = Copy::product( $companyId, $fromCompanyId );
                if( $tempResult == false )
                {
                    return $this->ajaxFail( '复制失败' );
                }
                break;
            default:
                return $this->ajaxFail( '未选择任何复制选项' );
        }
        return $this->ajaxSuccess( '复制成功' );
    }
}
